<?php

namespace app\shop\model\ad;
use app\common\model\ad\Ad as AdModel;
/**
 * 广告模型
 */
class Ad extends AdModel
{

    /**
     *添加新记录
     * add
     * @param $data
     * bool
     * @return bool
     * @Author kevin
     * @date 2024/5/24 15:59
     */
    public function add($data)
    {
        $data['app_id'] = self::$app_id;

        if($data['region'] == 1){ //1=全部 2= 指定商户
            return $this->save($data);
        }else{
            $select_list = $data['select_list']??[];
            if($select_list){
                $all_data = [];
                $array = [];
                foreach ($select_list as $key => $value){
                    if(isset($array[$value['shop_supplier_id']])){ //去重
                        continue;
                    }else{
                        $array[$value['shop_supplier_id']] = $value['name'];
                    }
                    $all_data[$key]['title'] = $data['title'];
                    $all_data[$key]['category_id'] = $data['category_id'];
                    $all_data[$key]['image_id'] = $data['image_id'];
                    $all_data[$key]['sort'] = $data['sort'];
                    $all_data[$key]['link_url'] = $data['link_url'];
                    $all_data[$key]['name'] = $data['name'];
                    $all_data[$key]['type'] = $data['type'];
                    $all_data[$key]['img_size'] = $data['img_size'];
                    $all_data[$key]['shop_supplier_id'] = $value['shop_supplier_id'];
                    $all_data[$key]['province_id'] = $value['province_id'];
                    $all_data[$key]['city_id'] = $value['city_id'];
                    $all_data[$key]['county_id'] = $value['county_id'];
                    $all_data[$key]['audit_status'] = 1;
                    $all_data[$key]['market'] = $data['market'];
                    $all_data[$key]['app_id'] = self::$app_id;
                }
                if($all_data){
                    return $this->saveAll($all_data);
                }
            }
        }
    }


    /**
     * 编辑记录
     */
    public function edit($data)
    {
        return $this->save($data);
    }

    /**
     * 删除记录
     */
    public function remove()
    {
        return $this->delete();
    }
    /**
     * 获取广告总数量
     */
    public static function getAdTotal($where)
    {
        $model = new static;
        return $model->where($where)->count();
    }

    /**
     * 获取待审核数量
     */
    public static function getApplyCount()
    {
        return self::where('audit_status', '=', 0)->count();
    }
}